package com.siwei.mes.service.erp.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.siwei.mes.common.query.PageQuery;
import com.siwei.mes.common.util.JsonUtils;
import com.siwei.mes.entity.erp.Wblist;
import com.siwei.mes.entity.tgl.TyclInfo;
import com.siwei.mes.mapper.erp.WblistMapper;
import com.siwei.mes.mapper.tgl.TyclMapper;
import com.siwei.mes.service.erp.WblistService;
import com.siwei.mes.sync.erp.entity.SyncErpWaybill;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 磅单记录(Wblist)表服务实现类
 *
 * @author linzi
 * @since 2025-03-19 22:02:20
 */
@Slf4j
@Service
public class WblistServiceImpl implements WblistService {
    @Resource
    private WblistMapper wblistMapper;
    @Resource
    private TyclMapper tyclMapper;

    @Override
    public PageInfo<Wblist> getPage(PageQuery<Wblist, Wblist> query) {
        Wblist searchParam = query.getParams();
        return PageHelper.startPage(query.getPageNum(), query.getPageSize())
                .doSelectPageInfo(() -> wblistMapper.getList(searchParam));
    }

    @Override
    public Wblist loadById(Long id) {
        return wblistMapper.loadById(id);
    }

    @Override
    public List<Wblist> getAll(Wblist entity) {
        return wblistMapper.getList(entity);
    }

    @Override
    public List<SyncErpWaybill> getCompensateInfo(String time, String yclName, String pzgg) {
        log.info("开始查询补偿原材料的数据，查询时间：【{}】之后！原材料名称：【{}】,原材料规格：【{}】", time, yclName, pzgg);
        List<SyncErpWaybill> list = wblistMapper.getCompensateInfo(time, yclName, pzgg);
        log.info("查询到【{}】条数据！开始获取对应原材料类型", list.size());
        if (!list.isEmpty()) {
            Map<String, String> tyclLbMap = this.getTyclLbMap();
            for (SyncErpWaybill erpWaybill : list) {
                String key = erpWaybill.getYclName() + "_" + erpWaybill.getPzgg();
                if (tyclLbMap.containsKey(key)) {
                    erpWaybill.setCategory(tyclLbMap.get(key));
                }
            }
        }
        return list;
    }

    @Override
    public List<SyncErpWaybill> getCompensateInfoByWlIdList(List<String> idList) {
        log.info("开始查询补偿原材料的数据，查询入参：【{}】", JsonUtils.getJson(idList));
        List<SyncErpWaybill> list = wblistMapper.getCompensateInfoByWlIdList(idList);
        log.info("查询到【{}】条数据！开始获取对应原材料类型", list.size());
        if (!list.isEmpty()) {
            Map<String, String> tyclLbMap = this.getTyclLbMap();
            for (SyncErpWaybill erpWaybill : list) {
                String key = erpWaybill.getYclName() + "_" + erpWaybill.getPzgg();
                if (tyclLbMap.containsKey(key)) {
                    erpWaybill.setCategory(tyclLbMap.get(key));
                }
            }
        }
        return list;
    }

    /**
     * 获取所有tgl原材料类别  key:ylmc_pzgg  value:lb
     *
     * @return
     */
    public Map<String, String> getTyclLbMap() {
        Map<String, String> map = new HashMap<>();
        List<TyclInfo> list = tyclMapper.getTyclAll();
        for (TyclInfo tyclInfo : list) {
            String key = tyclInfo.getYlmc() + "_" + tyclInfo.getPzgg();
            map.put(key, tyclInfo.getLb());
        }
        return map;
    }
}
